import { BrowserWindow, nativeTheme } from 'electron';
import icon from '@assets/icon.png?asset'


import {
    LAUNCHER_WIDTH,
    LAUNCHER_HEIGHT
} from '@shared/constans/common';


export default () => {
  let win: any;

  const init = (parent_win: BrowserWindow) => {
    createWindow(parent_win);
  };

  const createWindow = async (parent_win: BrowserWindow) => {
    win = new BrowserWindow({
      height: LAUNCHER_HEIGHT,
      minHeight: LAUNCHER_HEIGHT,
      useContentSize: true,
      resizable: true,
      width: LAUNCHER_WIDTH,
      maxWidth: LAUNCHER_WIDTH + 200,
      minWidth: LAUNCHER_WIDTH - 100,
      title: 'APPLauncher',
      autoHideMenuBar: true,
      ...(process.platform === 'linux' ? { icon } : {}),
      backgroundColor: nativeTheme.shouldUseDarkColors ? '#1c1c28' : '#fff',
      parent: parent_win,
      webPreferences: {
        webSecurity: false,
        backgroundThrottling: false,
        contextIsolation: false,
        webviewTag: true,
        nodeIntegration: true,
        spellcheck: false,
        sandbox: false
      },
    });

    if (process.env.NODE_ENV === 'development') {
      // Load the url of the dev server if in development mode
      win.loadURL("http://localhost:5173/");
    } else {
      win.loadURL('app://bundle/../launcher/index.html');
    }

    win.on('closed', () => {
      win = undefined;
    });

    win.on('show', () => {
      // versonHandler.checkUpdate();
      // win.webContents.openDevTools();
    });
    win.once('ready-to-show', async () => {
        
    })

    win.on('hide', () => {

    });

  };

  const getWindow = () => win;


  return {
    init,
    getWindow,
  };
};
